﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel.Data;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel;

public partial class Add_State : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();

    Country country = new Country();
    State state = new State();


    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            getCountry();


            if (string.IsNullOrEmpty(Request.QueryString["Id"]) == false)
            {
                lblHeader.Text = "Edit State";
                BindData();
            }
            else
            {
                lblHeader.Text = "Add State";
            }
        }
    }
    #endregion

    #region save clear and cancel
    protected void Save(object sender, EventArgs e)
    {
        State state = new State();
       
        try
        {
          
            state = new State(_dataObject);
            BuildObject(state);
            state.GetStateByName();
           
            if (Request.QueryString["Id"] != null)
            {
                if (state.Id != Convert.ToInt32(Request.QueryString["Id"]) && state.Id != 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> State is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger");
                    return;
                }
                state.Id = Convert.ToInt32(Request.QueryString["Id"]);
                state.IsDeleted = false;
                state.Update();
                divMessage.InnerHtml = "<strong>Success!</strong> state is updated successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
            }
            else
            {
                if (state.Id > 0)
                {
                    divMessage.InnerHtml = "<strong>Error!</strong> State is already Exists.";
                    divMessage.Attributes.Add("class", "alert alert-danger"); return;
                }
                BuildObject(state);
                state.CreatedDate = System.DateTime.Now;
                state.Create();

                divMessage.InnerHtml = "<strong>Success!</strong> state is added successfully.";
                divMessage.Attributes.Add("class", "alert alert-success");
                ClearControls();
            }
            

        }
        catch (Exception ex)
        {
        }
    }

    private void ClearControls()
    {
        drpCountry.ClearSelection();
        txtStateName.Text = string.Empty;
        drpCountry.Enabled = true;

    }
    #endregion

    #region getdata
    private void getCountry()
    {
        List<Country> countries = new List<Country>();
        country = new Country(_dataObject);

        countries = country.GetAllCountry();

        if (countries != null)
        {
            drpCountry.DataSource = countries;
            drpCountry.DataBind();
            drpCountry.Items.Insert(0, new ListItem("Select", "0"));
        }

    }
    private void BindData()
    {
        state = new State(_dataObject);
        // List<State> states = new List<State>();
        state.Id = Convert.ToInt32(Request.QueryString["Id"]);
        state.GetStateById();
        txtStateName.Text = state.Name;
        drpCountry.SelectedValue = state.CountryId.ToString();
        drpCountry.Enabled = false;
    }
    private void BuildObject(State city)
    {
        city.Name = txtStateName.Text;
        city.CountryId = Convert.ToInt32(drpCountry.SelectedValue);
    }
   
    #endregion
}